package com.itheima.app.manager;

import cn.hutool.core.collection.CollUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.*;
import com.itheima.service.db.BlackListService;
import com.itheima.service.db.NotificationService;
import com.itheima.service.db.QuestionService;
import com.itheima.service.db.UserInfoService;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.SettingVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.ArrayList;
import java.util.List;

@Component
public class SettingManager {

    @DubboReference
    QuestionService questionService;

    @DubboReference
    NotificationService notificationService;

    @DubboReference
    BlackListService blackListService;

    @DubboReference
    UserInfoService userInfoService;

    //查询用户通知设置
    public ResponseEntity findSetting() {
        //1.获取当前用户
        User user = UserHolder.get();

        //2.远程调用service查询陌生人问题
        Question question = questionService.findByUserId(user.getId());
        //3.远程调用service查询通知设置
        Notification notification = notificationService.findByUserId(user.getId());

        //4.封装vo对象且返回
        SettingVo vo = new SettingVo();
        vo.setId(user.getId());
        vo.setPhone(user.getMobile());

        //设置陌生人问题
        if (question != null) {
            vo.setStrangerQuestion(question.getStrangerQuestion());
        }

        //设置 通知设置
        if (notification != null) {
            vo.setPinglunNotification(notification.getPinglunNotification());
            vo.setLikeNotification(notification.getLikeNotification());
            vo.setGonggaoNotification(notification.getGonggaoNotification());
        }

        return ResponseEntity.ok(vo);
    }

    // 设置陌生人问题
    public void setQuestion(String content) {
        // 1.获取线程内userId
        Long userId = UserHolder.get().getId();
        // 2.根据userId查陌生人问题
        Question question = questionService.findByUserId(userId);
        // 3.判断处理
        if (question == null) {
            //4.若为空,就新增
            question = new Question();
            question.setUserId(userId);
            question.setStrangerQuestion(content);
            questionService.save(question);
        } else {
            //5.若不为空,就更新
            question.setStrangerQuestion(content);
            questionService.update(question);
        }
    }

    // 设置推送通知
    public void setNotification(Notification param) {
        //1.获取当前登录者id
        Long userId = UserHolder.get().getId();

        //2.根据用户id查询之前用户的通知设置
        Notification notification = notificationService.findByUserId(userId);

        //3.判断通知对象是否为空
        if (notification == null) {
            //3.1 若为空,则新增
            param.setUserId(userId);
            notificationService.save(param);
        } else {
            //3.2 否则则修改
            param.setId(notification.getId());
            notificationService.update(param);
        }
    }

    //黑名单列表
    public ResponseEntity findBlackListByPage(Integer pageNum, Integer pageSize) {
        //1.获取登录者id
        Long userId = UserHolder.get().getId();

        //2.调用service分页查询,返回pageBeanVo(集合中的黑名单只用用户id)
        PageBeanVo pageBeanVo = blackListService.findByPage(pageNum, pageSize, userId);

        //3.获取pageBeanVo中黑名单id集合
        List<BlackList> blackIdlist = (List<BlackList>) pageBeanVo.getItems();
        //4.调用userInfoService查询黑名单用户信息,将userInfo放入一个新集合中userInfoList
        //4.1声明userInfoList
        List<UserInfo> userInfoList = new ArrayList<>();

        //4.2遍历blackIdlist,获取黑名单用户id,通过id查询userInfo,将userInfo存入userInfoList
        if (CollUtil.isNotEmpty(blackIdlist)) {
            blackIdlist.forEach(blackList -> {
                UserInfo userInfo = userInfoService.findById(blackList.getBlackUserId());
                userInfoList.add(userInfo);
            });
        }

        //5.将userInfo设置给pageBeanVo
        pageBeanVo.setItems(userInfoList);
        //6.返回pageBeanVo
        return ResponseEntity.ok(pageBeanVo);
    }

    //移除黑名单
    public void deleteBlackList(Long blackId) {
        //1.获取当前用户id
        //2.调用service删除黑名单
        blackListService.delete(UserHolder.get().getId(), blackId);
    }
}
